<template>
  <div class="app-container">

    <template>
      <h1 style="color: #87ceeb; text-align: left; font-weight: bold;">考试大纲基本信息</h1>
    </template>
    <hr>



    <div style="display: flex;align-items: center">
      <div style="flex: 2" >
        <div><h1 >学 年: {{ person.tbAcademicYear.academicYearName }}</h1></div>
<!--        <div><h1 >学 年: {{ person.academicYearName }}</h1></div> 这可以这样写-->

        <div><h1 >教研组: {{ person.tbTeachingResearchSection.teachingResearchSectionName}}</h1></div>
      </div>

      <div style="flex: 2" >
        <h1 align="left">学期: {{ person.tbSemester.semesterName }}</h1>
        <h1 align="left">考试方式: {{ person.tbAssessmentMethod.assessmentMethodName}}</h1>
      </div>
      <div style="flex: 2">
        <h1 align="left">科目: {{ person.tbSubject.subjectName }}</h1>
        <h1 align="left">编写教师: {{ person.tbTeacher.teacherName }}</h1>
      </div>
    </div>


    <template>
      <h1 style="color: #87ceeb; text-align: left;  font-weight: bold;">考试详情</h1>
    </template>
    <hr>

    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="考试单元">
        <el-input
          v-model="queryParams['tbUnits.unitsName']"
          placeholder="请输入单元ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="知识点">
        <el-input
          v-model="queryParams['tbKnowledgePoints.knowledgePointsName']"
          placeholder="请输入知识点"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="考试要求目标" label-width="100px">
        <el-input
          v-model="queryParams['tbExamRequirements.examRequirementsTarget']"
          placeholder="请输入考试要求目标"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['exam_syllabus:details:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['exam_syllabus:details:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['exam_syllabus:details:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['exam_syllabus:details:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="detailsList" :header-cell-style="{ background: '#87ceeb' }"
              border  :row-class-name="tableRowClassName"  @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column   label="序号"  align="center"  type="index" :index="indexMethod"></el-table-column>
      <el-table-column label="考试单元" align="center" prop="tbUnits.unitsName" />
      <el-table-column label="知识点" align="center" prop="tbKnowledgePoints.knowledgePointsName" />
      <el-table-column label="考试要求目标" align="center" prop="tbExamRequirements.examRequirementsTarget" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            type="primary"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['exam_syllabus:details:edit']"
          >修改</el-button>
          <el-button
            type="danger"
            @click="handleDelete(scope.row)"
            v-hasPermi="['exam_syllabus:details:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改考试详情对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
<!--        <el-form-item label="单元ID" prop="unitsId">
          <el-input v-model="form.unitsId" placeholder="请输入单元ID" />
        </el-form-item>-->

        <el-form-item label="考试单元" prop="unitsId">
          <el-select v-model="form.unitsId" placeholder="请输入考试单元" clearable>
            <!--label这里面的东西才是我们要拿的-->
            <el-option
              v-for="item in unitsList"
              :key="item.unitsId"
              :label="item.unitsName"
              :value="item.unitsId"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="知识点" prop="knowledgePointsId">
          <el-select v-model="form.knowledgePointsId" placeholder="请输入考试单元" clearable>
            <!--label这里面的东西才是我们要拿的-->
            <el-option
              v-for="item in knowledgePointsList"
              :key="item.knowledgePointsId"
              :label="item.knowledgePointsName"
              :value="item.knowledgePointsId"
            ></el-option>
          </el-select>
        </el-form-item>


<!--        <el-form-item label="考试要求目标ID" prop="examRequirementsId">
          <el-input v-model="form.examRequirementsId" placeholder="请输入考试要求目标ID" />
        </el-form-item>-->

        <el-form-item label="考试要求目标" prop="examRequirementsId" label-width="110px">
          <el-select v-model="form.examRequirementsId" placeholder="请输入考试要求目标"  clearable>
            <!--label这里面的东西才是我们要拿的-->
            <el-option
              v-for="item in examRequirementsList"
              :key="item.examRequirementsId"
              :label="item.examRequirementsTarget"
              :value="item.examRequirementsId"
            ></el-option>
          </el-select>
        </el-form-item>

<!--        <el-form-item label="考试大纲设置ID" prop="examSyllabusId">
          <el-input v-model="form.examSyllabusId" placeholder="请输入考试大纲设置ID" />
        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDetails, getDetails, delDetails, addDetails, updateDetails } from "@/api/exam_syllabus/details";
import {listUnits} from "@/api/basice_info/units";
import {listPoints} from "@/api/basice_info/points";
import {listRequirements} from "@/api/basice_info/requirements";
import {getSyllabus} from "@/api/exam_syllabus/syllabus";


export default {
  name: "Details",
  data() {

    return {
      person:{
        examSyllabusId:null,
        academicYearName: '2023-2024',
        semesterName: "第一学期",
        subjectName: "数学",
        teachingResearchSectionName:"数学教研组",
        teacherName: "教师1",
        assessmentMethodName: "考试"
         /* academicYearName: null,
          semesterName: null,
          subjectName: null,
          teachingResearchSectionName: null,
          teacherName: null,
          assessmentMethodName: null*/
       },

      // 考试大纲
      syllabusList: [],

      // 考试目标要求
      examRequirementsList: [],
      // 知识点
      knowledgePointsList: [],
      // 考试单元
      unitsList: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 考试详情表格数据
      detailsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        unitsId: null,
        knowledgePointsId: null,
        examRequirementsId: null,
        'tbUnits.unitsName': null,
        'tbKnowledgePoints.knowledgePointsName': null,
        'tbExamRequirements.examRequirementsTarget': null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        unitsId: [
          { required: true, message: "单元ID不能为空", trigger: "blur" }
        ],
        knowledgePointsId: [
          { required: true, message: "知识点ID不能为空", trigger: "blur" }
        ],
        examRequirementsId: [
          { required: true, message: "考试要求目标ID不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    // 获取页面传过来的参数
    this.examSyllabusId = this.$route.query.examSyllabusId;
    this.getSyllabusById();
    this.getList();
    this.getknowledgePointsList();
    this.getunitsList();
    this.getexamRequirementsList();

  },
  methods: {
    // 序号
    indexMethod(index) {
      return (
        (this.queryParams.pageNum - 1) * this.queryParams.pageSize + index + 1
      );
    },

    getSyllabusById() {
      getSyllabus(this.examSyllabusId).then((response) => {
        this.person = response.data;
        console.log(this.person)
      });
    },
    // 知识点
    getknowledgePointsList() {
      listPoints().then(response => {
        this.knowledgePointsList = response.rows;
      });
    },

    // 考试单元
    getunitsList() {
      listUnits().then(response => {
        this.unitsList = response.rows;

      });
    },

    // 考试目标要求
    getexamRequirementsList() {
      listRequirements().then(response => {
        this.examRequirementsList = response.rows;
      });
    },

    // 表格颜色
    tableRowClassName({row, rowIndex}) {
      if (rowIndex % 2 === 0) {
        return 'success-row';
      } else {
        return 'warning-row';
      }
    },
    /** 查询考试详情列表 */
    getList() {
      this.loading = true;
      listDetails(this.queryParams).then(response => {
        this.detailsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        examDetailsId: null,
        unitsId: null,
        knowledgePointsId: null,
        examRequirementsId: null,
        examSyllabusId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.examDetailsId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加考试详情";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const examDetailsId = row.examDetailsId || this.ids
      getDetails(examDetailsId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改考试详情";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.examDetailsId != null) {
            updateDetails(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDetails(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const examDetailsIds = row.examDetailsId || this.ids;
      this.$modal.confirm('是否确认删除考试详情编号为"' + examDetailsIds + '"的数据项？').then(function() {
        return delDetails(examDetailsIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('exam_syllabus/details/export', {
        ...this.queryParams
      }, `details_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>

<style>

.el-table .warning-row {
  background: oldlace;
}

.el-table .success-row {
  background: #f0f9eb;
}


</style>
